package org.example.springai_deepseek.utils;

import org.springframework.ai.tool.annotation.Tool;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class MyTools {

    @Tool(description = "Get the weather in location")
    public String getWeatherInLocation(String name) {
        System.out.println("===>天气查询工具 " + name);
        return "阴天, 26°C, 空气湿度: 60%";
    }


    @Tool(description = "当需要获得windows信息或可以通过命令行操作达到目的的时候,请执行windows-cmd命令")
    public String execWindowsCmd(String cmd) {
        System.out.println(">>>" + cmd);
        // Runtime
        try {
            Process process = Runtime.getRuntime().exec("cmd /c " + cmd);
            process.waitFor();
            StringBuilder output = new StringBuilder();
            String line;
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
                while ((line = reader.readLine()) != null) {
                    output.append(line).append("\n");
                }
            }
            // UTF-8编码
            System.out.println("<<<" + output);
            return output.toString();
        } catch (IOException|InterruptedException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Tool(description = "通过ffmpeg命令对视频音频图片等媒体文件进行处理")
    public String ffmpeg(String cmd) {
        return execWindowsCmd(cmd);
    }

}